package cn.strategy.fund;

import cn.model.Mmaidian;
import cn.model.Mmstock;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Mai_dian_case3 {
    @Test
    public void show_mai_dian() throws ClassNotFoundException, SQLException {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/data;AUTO_SERVER=TRUE", "root", "root");

        String sql = "SELECT symbol FROM type_stock GROUP BY symbol";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            //getData_ex(conn, rs.getString(1));
            getHL(conn, rs.getString(1));
        }

        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
    }

    public void getHL(Connection conn, String symbol) {
        String sql = "SELECT price, h, stock_date, m_index, FROM mai_dian WHERE SYMBOL = '"+ symbol +"' AND M_INDEX > 0 ORDER BY STOCK_DATE DESC";

        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<Mmaidian> lst = new ArrayList<>();
        int count = 0;

        try {
            stmt = conn.prepareStatement(sql);

            // 5. 执行查询并获取结果集
            rs = stmt.executeQuery();

            // 6. 遍历结果集并处理数据
            while (rs.next()) {
                if (count <= 10) {
                    count++;
                } else {
                    break;
                }

                lst.add(new Mmaidian(rs.getDouble(1), rs.getBoolean(2), rs.getDate(3), rs.getInt(4)));
            }

            Mmstock mmstock = getLastPrice(conn, symbol);
            getHL1(symbol, mmstock, lst);

        } catch (SQLException e) {
            System.err.println("数据库操作失败！错误信息：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public Mmaidian frist(List<Mmaidian> lstmd) {
        if (!lstmd.isEmpty()) {
            return lstmd.get(0);
        }
        return null;
    }

    public List<Mmaidian> rest(List<Mmaidian> lstmd) {
        if (!lstmd.isEmpty()) {
            return lstmd.subList(1, lstmd.size());
        }
        return null;
    }

    public void getHL1(String symbol, Mmstock mmstock, List<Mmaidian> lst) {
        getHL1(symbol, mmstock, frist(lst), rest(lst));
    }

    public void getHL1(String symbol, Mmstock mmstock, Mmaidian f, List<Mmaidian> lst) {
        if (f != null) {
            if (f.getH() == false) {
                Mmaidian m = frist(lst);
                if (m != null) {
                    if (m.getH() == true) {
                        if (m.getM_index() - f.getM_index() > 7 && m.getPrice() / f.getPrice() > 1.08D && mmstock.getZhangdie() > -0.6D && mmstock.getZhangdie() < 0.6D) {
                            System.out.println(symbol + " " + mmstock.getName());
                        }
                    } else {
                        if (f.getPrice() < m.getPrice()) {
                            getHL1(symbol, mmstock, m, rest(lst));
                        }
                    }
                }
            }
        }
    }

    public Mmstock getLastPrice(Connection conn, String symbol) {
        String sql = "SELECT PRICE, NAME, zhangdie FROM df_stock_date WHERE SYMBOL = '"+ symbol +"' ORDER BY STOCK_DATE DESC LIMIT 0, 1";

        PreparedStatement stmt = null;
        ResultSet rs = null;
        Mmstock last_price = new Mmstock();

        try {
            stmt = conn.prepareStatement(sql);

            // 5. 执行查询并获取结果集
            rs = stmt.executeQuery();

            // 6. 遍历结果集并处理数据
            while (rs.next()) {
                last_price.setPrice(rs.getDouble(1));
                last_price.setName(rs.getString(2));
                last_price.setZhangdie(rs.getDouble(3));
            }

        } catch (SQLException e) {
            System.err.println("数据库操作失败！错误信息：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return last_price;
    }
}




















































